import random
import string
import hashlib
import time
import re


def random_digits_string(n):
    return ''.join(random.choices(string.digits, k=n))


def random_hex_string(n):
    return ''.join(random.choices(string.hexdigits, k=n))


def random_punctuation_string(n):
    return ''.join(random.choices(string.punctuation, k=n))


def is_numeric(s):
    try:
        float(s)
        return True
    except ValueError:
        pass

    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
    return False


def md5(content):
    m = hashlib.md5()
    m.update(content)
    return m.hexdigest()

def camel_to_snake(name: str) -> str:
    s1 = re.sub(r'(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

def snake_to_camel(name: str) -> str:
    parts = name.split('_')
    return parts[0] + ''.join(word.capitalize() for word in parts[1:])

# json 驼峰法 和 snake格式的转换
def convert_keys(obj, to: str = 'snake'):
    """
    Recursively converts dictionary keys between camelCase and snake_case.

    Args:
        obj: The input object (dict, list, or other)
        to: 'snake' or 'camel'

    Returns:
        New object with converted keys.
    """
    if isinstance(obj, dict):
        converter = camel_to_snake if to == 'snake' else snake_to_camel
        return {converter(k): convert_keys(v, to) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [convert_keys(i, to) for i in obj]
    else:
        return obj

